<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>Title</title>
    <link rel="stylesheet" href="layui/css/layui.css">
    <script src="layui/layui.js"></script>
</head>
<!-- L2Dwidget.js L2D网页动画人物 -->
<script src="https://eqcn.ajz.miesnfu.com/wp-content/plugins/wp-3d-pony/live2dw/lib/L2Dwidget.min.js"></script>
<script>
    L2Dwidget.init({
        "model": {
            "jsonPath": "https://unpkg.com/live2d-widget-model-koharu@1.0.5/assets/koharu.model.json",
            "scale": 1,
            "hHeadPos": 0.5,
            "vHeadPos": 0.618
        },
        "display": {"position": "right", "width": 250, "height": 300, "hOffset": 0, "vOffset": 0},
        "mobile": {"show": true, "scale": 0.5},
        "react": {"opacityDefault": 0.7, "opacityOnHover": 0.2}
    });
</script>
<body>
<div style="margin: 0 40% ;color: #4E5465;font-size: 50px ">生成销售单</div>

<div class="demoTable" style="display: inline-block">
    <form id="AAA" style="display: inline-block">
        <%--<div class="layui-inline">--%>
            <%--<input class="layui-input" name="bookid" placeholder="id" id="demoReload" autocomplete="off">--%>
        <%--</div>--%>
        <div class="layui-inline">
            <input class="layui-input" name="bookname" placeholder="书名" id="demoReload1" autocomplete="off">
        </div>
        <div class="layui-inline">
            <input class="layui-input" name="author" id="demoReload2" placeholder="作者" autocomplete="off">
        </div>
        <div class="layui-inline">
            <input  class="layui-input" name="press" id="demoReload3" placeholder="出版社"
                   autocomplete="off">
        </div>
    </form>
    <button class="layui-btn" data-type="reload"><i class="layui-icon layui-icon-search"></i></button>
</div>
<button class="layui-btn" data-type="reload" id="btn1"><i class="layui-icon layui-icon-refresh"></i></button>


<div class="layui-row">
    <div class="layui-col-xs6">
        <div class="grid-demo grid-demo-bg1">
            <table id="test" lay-filter="test"></table>
            <script type="text/html" id="toolbarDemo">
                <div class="layui-btn-container">
                    <button class="layui-btn layui-btn-sm" lay-event="getCheckData">添加采购订单</button>
                </div>
            </script>
            <script type="text/html" id="barDemo">
                <a class="layui-btn layui-btn-xs" lay-event="edit1">编辑</a>
                <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="del">删除</a>
            </script>
        </div>
    </div>
    <div class="layui-col-xs6">
        <div class="grid-demo">
            <table id="test1" lay-filter="test1"></table>
            <script type="text/html" id="toolbarDemo1">
                <div class="layui-btn-container">
                    <button class="layui-btn layui-btn-sm" lay-event="getCheckData">生成采购订单</button>
                    <button class="layui-btn layui-btn-sm" lay-event="del">清空采购订单</button>
                </div>
            </script>
            <script type="text/html" id="barDemo1">
                <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="del">删除</a>
            </script>
        </div>
    </div>
</div>

</body>
<script>
    layui.use(['table', 'jquery', 'form', 'layer'], function () {
        var table = layui.table;
        var $ = layui.jquery;
        var form = layui.form;
        var layer = layui.layer;

        $(function () {
            showTable();
            showTable1();
        })

        var arr = [];
        var v = false;
        function showTable() {
            //第一个实例
            table.render({
                elem: '#test'
                , url: '${pageContext.request.contextPath}/book/query'
                , toolbar: '#toolbarDemo' //开启头部工具栏，并为其绑定左侧模板
                , defaultToolbar: ['filter', 'exports', 'print', { //自定义头部工具栏右侧图标。如无需自定义，去除该参数即可
                    title: '提示'
                    , layEvent: 'LAYTABLE_TIPS'
                    , icon: 'layui-icon-tips'
                }]
                , title: '用户数据表'
                , cols: [[
                    {type: 'checkbox', fixed: 'left'},
                    {field: 'bookid', title: '书ID', fixed: 'left', unresize: true, sort: true}
                    , {field: 'bookname', title: '书名'}
                    , {field: 'author', title: '作者', sort: true}
                    , {field: 'count', title: '库存'}
                    , {field: 'type', title: '类别'}
                    , {field: 'press', title: '出版社', sort: true}
                    , {field: 'outprice', title: '售价', sort: true}
                ]]
                , id: 'testReload'
                , page: true
                , limit: 8
                , limits: [8, 10,15]
            });

        }

        function showTable1() {
            //第一个实例
            table.render({
                elem: '#test1'
                , data: arr
                , toolbar: '#toolbarDemo1' //开启头部工具栏，并为其绑定左侧模板
                , defaultToolbar: ['filter', 'exports', 'print', { //自定义头部工具栏右侧图标。如无需自定义，去除该参数即可
                    title: '提示'
                    , layEvent: 'LAYTABLE_TIPS'
                    , icon: 'layui-icon-tips'
                }]
                , title: '用户数据表'
                , cols: [[
                    {type: 'checkbox', fixed: 'left'},
                    {field: 'bookid', title: '书ID', fixed: 'left', unresize: true, sort: true}
                    , {field: 'bookname', title: '书名'}
                    , {field: 'author', title: '作者', sort: true}
                    , {field: 'count', title: '库存'}
                    , {field: 'plancount', title: '订购数量', edit: 'text'}
                    , {field: 'type', title: '类别'}
                    , {field: 'press', title: '出版社', sort: true}
                    , {field: 'outprice', title: '售价', sort: true}
                    , {fixed: 'right', title: '操作', toolbar: '#barDemo1'}
                ]]
                , page: true
            });

        }


        //头工具栏事件
        table.on('toolbar(test)', function (obj) {
            var checkStatus = table.checkStatus(obj.config.id);
            switch (obj.event) {
                case 'getCheckData':
                    var data = checkStatus.data;
                    $.each(data, function (v, value) {
                        var a = "";
                        for (var i = 0; i < arr.length; i++) {
                            if (arr[i].bookid == value.bookid) {
                                a = "ok";
                            }
                        }
                        if (a == "ok") {
                            layer.msg("已存在该数据", {icon: 4})
                        } else {
                            arr.push(value);
                        }

                    });
                    showTable1();
                    break;
            }
            ;
        });


        //头工具栏事件
        table.on('toolbar(test1)', function (obj) {
                var checkStatus = table.checkStatus(obj.config.id);
                switch (obj.event) {
                    case 'getCheckData':
                        var data = checkStatus.data;

                        var s = "";
                        for (var i = 0; i < data.length; i++) {
                            if (data[i].plancount == undefined) {
                                s = "ok";
                            }
                        }
                        if (s == "ok") {
                            layer.msg("有数据没有被选中", {icon: 5})
                        } else if(v){
                            $.ajax({
                                type: 'post',
                                url: '${pageContext.request.contextPath}/oorder/sale',
                                data: JSON.stringify(data),
                                contentType: 'application/json;charset=utf-8',
                                success: function (result) {
                                    if (result.code == 1) {
                                        layer.msg(result.msg, {icon: 1});
                                        arr.splice(0);
                                        showTable1();
                                    } else {
                                        layer.msg(result.msg, {icon: 5});
                                    }
                                }
                            });

                        }else {
                            layer.msg("数据格式古不对",{icon:5});
                        }
                        break;
                    case 'del':
                        arr.splice(0);
                        showTable1();
                        break;

                }

            }
        );

        //监听行工具事件
        table.on('tool(test1)', function (obj) {
            var data = obj.data;
            //console.log(obj)
            if (obj.event === 'del') {
                layer.confirm('真的删除行么', function (index) {
                    for (var i = 0; i < arr.length; i++) {
                        if(arr[i].bookid==data.bookid){
                            arr.splice(i,1);
                            showTable();
                        }
                    }
                    obj.del();
                    layer.close(index);
                });
            } else if (obj.event === 'edit') {
                layer.prompt({
                    formType: 2
                    , value: data.email
                }, function (value, index) {
                    obj.update({
                        email: value
                    });
                    layer.close(index);
                });
            }
        });


        table.on('edit(test1)', function (obj) { //注：edit是固定事件名，test是table原始容器的属性 lay-filter="对应的值"
            v = false;
            var data = obj.data;
            var reg = /^[1-9]\d*$/;
            if (!reg.test(obj.value)) {
                layer.msg("数量为正整数!", {icon: 5, anim: 6});

            } else if (obj.value > data.count) {
                layer.msg("不能大于库存", {icon: 5, anim: 6});
            }else {
                v=true;
            }
        });

        <%--function showSelect() {--%>
        <%--$.ajax({--%>
        <%--type: "get",--%>
        <%--url: '${pageContext.request.contextPath}/book/type',--%>
        <%--dataType: 'json',//根据需求分析是否需要写--%>
        <%--success: function (result) {--%>
        <%--//alert(JSON.stringify(result));--%>
        <%--for (var i = 0; i < result.length; i++) {--%>
        <%--$("#typeid").append(--%>
        <%--$("<option>").attr("value", result[i]['typeid']).text(result[i]['type'])--%>
        <%--)--%>
        <%--}--%>
        <%--//alert($("#typeid").html())--%>
        <%--//更新渲染下拉框数据--%>
        <%--form.render('select', 'formData');--%>
        <%--}--%>
        <%--})--%>
        <%--}--%>

        <%--//表单提交--%>
        <%--form.on('submit(submitForm)', function (data) {--%>

            <%--$.ajax({--%>
                <%--type: 'post',--%>
                <%--url: '${pageContext.request.contextPath}/book/update',--%>
                <%--data: data.field,--%>
                <%--dataType: 'json',--%>
                <%--success: function (result) {--%>
                    <%--layer.closeAll();--%>
                    <%--if (result.code == 1) {--%>
                        <%--//layer.msg  提示框--%>
                        <%--showTable();--%>
                        <%--layer.msg(result.msg);--%>
                    <%--} else {--%>
                        <%--layer.msg(result.msg);--%>
                    <%--}--%>

                <%--}--%>
            <%--});--%>
            <%--return false; //阻止表单跳转。如果需要表单跳转，去掉这段即可。--%>
        <%--});--%>

        $(document).on('click', '#btn1', function () {

            showTable();
            $("#AAA")[0].reset();

        });

        //条件查询的重载
        var $ = layui.$, active = {
            reload: function () {
                var demoReload = $('#demoReload').val();
                var demoReload1 = $('#demoReload1').val();
                var demoReload2 = $('#demoReload2').val();
                var demoReload3 = $('#demoReload3').val();
                //执行重载
                table.reload('testReload', {
                    page: {
                        curr: 1 //重新从第 1 页开始
                    }
                    , where: {
                        bookid: demoReload,
                        bookname: demoReload1,
                        author: demoReload2,
                        press: demoReload3
                    }
                }, 'data');
            }
        };

        $('.demoTable .layui-btn').on('click', function () {
            var type = $(this).data('type');
            active[type] ? active[type].call(this) : '';
        });

    })
    ;
</script>
</html>
